'use strict';

const EventEmitter = require('events').EventEmitter;

export default class extends EventEmitter {

    constructor(options){
        super();

        this.upComment       = null;

        this.downBroadcast   = null;

        this.connection      = null;

        // this.serverName      = options.serverName +'-'+ process.pid;

        // this.acceptQueueName = options.serverName +'-'+ options.uuid +'-'+ process.pid;

        this.serverName      = options.serverName;

        this.acceptQueueName = options.serverName;

        this._init(options.amqp, options.mq);

        // this._test();
    }
    
    _init (options, amqp){
        let self = this;

        let serverName = this.serverName;

        if(this.connection){
            this.connection.end();
            this.connection.destroy();
        }
        
        this.connection = amqp.createConnection(options);

        this.connection.on('error', (e)=>{
            this.emit('error', e);
        });

        this.connection.on('ready', function () {

            //发送弹幕exchange
            self.upComment = self.connection.exchange('up-command', {type: 'direct', autoDelete: false, durable: true});

            // 接收广播exchange
            self.downBroadcast = self.connection.exchange('down-broadcast', {exclusive: true, type: 'fanout', autoDelete: false, durable: true});
            
            //接收队列
            self.connection.queue(
                self.acceptQueueName, 
                {
                    exclusive: true, 
                    autoDelete: true, 
                    durable: false
                }, 
                function(queue){
                
                    queue.bind('down-broadcast', self.acceptQueueName);

                    queue.subscribe(function (message) {
                        try {
                            message.data = JSON.parse(message.data.toString('utf-8'));
                            self._receive( message ); 
                        } catch(e) {
                            think.log(e);
                        }
                        
                    });
                }
            );
            
        });
    }

    _test(){
        setInterval(()=>{
            this.publish({
                data: {
                    content: 'test-' + new Date().getTime(),
                    room: 'test'
                }
            });
        }, 5000)
    }   

    publish (msg, options){

        msg.source = this.serverName;

        this.upComment && this.upComment.publish('comment.send', JSON.stringify(msg),{
            contentType:'application/json'
        });

    }

    report (msg, type = 10){
        
        this.upComment && this.upComment.publish('command.send', JSON.stringify({
            type  : type,
            source: this.serverName,
            data  : Object.assign(msg, {
                server: this.serverName
            })
        }),{
            contentType:'application/json'
        });
    }

    _receive(message){
        this.emit('receive', message);
    }
}
